﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using GMUtilitiesDotNet.Helpers;
using GMUtilitiesDotNet.Events;
using GMUtilitiesDotNet.Services;
using GMUtilitiesDotNet.General;

namespace GMUtilitiesDotNet.ViewModels
{
    public abstract class ViewModelBase : DisposableBase, INotifyPropertyChanged
    {
        #region Properties
        public static readonly ServiceProvider ServiceProvider = new ServiceProvider();
        #endregion

        #region Events
        #region PropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChangedEvent(PropertyChangedEventArgs pArgs)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, pArgs);
            }
        }
        #endregion

        #region ActivateRequest
        public event EventHandler ActivateRequest;
        private void RaiseActivateRequestEvent()
        {
            var handler = ActivateRequest;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
        #endregion

        #region CloseRequest
        public event EventHandler<CloseRequestEventArgs> CloseRequest;
        protected void RaiseCloseRequestEvent(CloseRequestEventArgs pArgs)
        {
            var handler = CloseRequest;
            if (handler != null)
            {
                handler(this, pArgs);
            }
        }
        #endregion
        #endregion

        #region Constuctors
        public ViewModelBase()
        {
        }
        #endregion

        #region Methods
        protected static void Dispose(IDisposable pObj)
        {
            if (pObj != null)
            {
                pObj.Dispose();
            }
        }

#if DEBUG
        /// <summary>
        /// Useful for ensuring that ViewModel objects are properly garbage collected.
        /// </summary>
        ~ViewModelBase()
        {
            string msg = string.Format("{0} ({1}) Finalized", GetType().Name, GetHashCode());
            System.Diagnostics.Debug.WriteLine(msg);
        }
#endif
        #endregion
    }
}